#include "stm32f10x.h"
#include "delay.h"
#include "remote.h"
#include "usart.h"
#include "ir.h"
#include "key.h"
#include "led.h"

u16 Code_ON[] = {8482
                 , 4086
                 , 601
                 , 492
                 , 576
                 , 1568
                 , 577
                 , 1569
                 , 575
                 , 517
                 , 574
                 , 1569
                 , 577
                 , 517
                 , 575
                 , 1569
                 , 577
                 , 523
                 , 576
                 , 516
                 , 575
                 , 1569
                 , 579
                 , 516
                 , 574
                 , 1568
                 , 576
                 , 1569
                 , 578
                 , 1567
                 , 576
                 , 1569
                 , 576
                 , 526
                 , 573
                 , 517
                 , 575
                 , 516
                 , 575
                 , 520
                 , 574
                 , 517
                 , 576
                 , 517
                 , 578
                 , 516
                 , 572
                 , 517
                 , 575
                 , 523
                 , 577
                 , 517
                 , 576
                 , 516
                 , 573
                 , 517
                 , 573
                 , 520
                 , 577
                 , 515
                 , 574
                 , 519
                 , 575
                 , 518
                 , 576
                 , 523
                 , 574
                 , 519
                 , 573
                 , 526
                 , 565
                 , 518
                 , 573
                 , 519
                 , 575
                 , 1568
                 , 576
                 , 518
                 , 572
                 , 520
                 , 574
                 , 523
                 , 574
                 , 520
                 , 573
                 , 520
                 , 573
                 , 518
                 , 574
                 , 518
                 , 575
                 , 517
                 , 576
                 , 517
                 , 575
                 , 518
                 , 575
                 , 523
                 , 574
                 , 518
                 , 574
                 , 519
                 , 574
                 , 559
                 , 531
                 , 521
                 , 573
                 , 518
                 , 578
                 , 514
                 , 574
                 , 1569
                 , 576
                 , 525
                 , 574
                 , 518
                 , 574
                 , 519
                 , 572
                 , 521
                 , 578
                 , 512
                 , 572
                 , 518
                 , 574
                 , 531
                 , 564
                 , 516
                 , 576
                 , 524
                 , 575
                 , 520
                 , 568
                 , 518
                 , 574
                 , 522
                 , 572
                 , 518
                 , 571
                 , 516
                 , 576
                 , 523
                 , 565
                 , 520
                 , 575
                 , 527
                 , 610
                 , 476
                 , 575
                 , 517
                 , 577
                 , 514
                 , 576
                 , 517
                 , 574
                 , 522
                 , 574
                 , 515
                 , 574
                 , 516
                 , 574
                 , 524
                 , 575
                 , 516
                 , 575
                 , 517
                 , 578
                 , 514
                 , 576
                 , 516
                 , 576
                 , 516
                 , 582
                 , 518
                 , 651
                 , 434
                 , 577
                 , 522
                 , 582
                 , 535
                 , 549
                 , 516
                 , 575
                 , 518
                 , 576
                 , 517
                 , 575
                 , 517
                 , 575
                 , 516
                 , 576
                 , 517
                 , 579
                 , 522
                 , 573
                 , 516
                 , 575
                 , 517
                 , 575
                 , 517
                 , 576
                 , 520
                 , 598
                 , 490
                 , 576
                 , 516
                 , 577
                 , 520
                 , 575
                 , 520
                 , 576
                 , 519
                 , 574
                 , 514
                 , 575
                 , 517
                 , 576
                 , 515
                 , 578
                 , 517
                 , 576
                 , 514
                 , 575
                 , 518
                 , 601
                 , 496
                 , 577
                 , 1600
                 , 546
                 , 514
                 , 601
                 , 490
                 , 576
                 , 516
                 , 575
                 , 523
                 , 596
                 , 1540
                 , 581
                 , 512
                 , 576
                 , 519
                 , 606
                },
                Code_OFF[] = {8452,     4143,      520,      574,      518,     1626,
                              519,     1626,      519,      573,      520,     1628,
                              522,      569,      518,     1626,      520,      553,
                              545,      547,      545,     1599,      547,      546,
                              546,     1597,      549,     1597,      547,     1596,
                              549,     1597,      549,      548,      552,      541,
                              553,      542,      550,      541,      548,      544,
                              551,      541,      553,      539,      550,      543,
                              554,      545,      552,      541,      553,      542,
                              551,      541,      548,      545,      549,      543,
                              549,      543,      549,      543,      548,      580,
                              548,      514,      548,      544,      579,      513,
                              582,      513,      545,     1601,      572,      514,
                              550,      543,      580,      518,      549,      544,
                              547,      544,      569,      522,      549,      543,
                              551,      543,      549,      543,      549,     1593,
                              553,     1606,      574,      509,      584,      506,
                              555,      538,      553,      538,      551,      542,
                              554,      540,      551,      545,      569,      693,
                              382,      545,      548,      543,      548,      556,
                              536,      544,      550,      542,      550,      543,
                              548,      545,      547,      551,      546,      545,
                              547,      545,      549,      544,      547,      578,
                              515,      545,      547,      546,      545,      574,
                              520,      579,      519,      545,      546,      546,
                              546,      546,      546,      573,      519,      546,
                              547,      573,      518,      548,      545,      579,
                              519,      547,      547,      572,      518,      574,
                              522,      571,      517,      574,      519,      546,
                              556,      564,      516,      581,      520,      572,
                              521,      545,      546,      582,      511,      573,
                              518,      550,      542,      547,      550,      544,
                              545,      553,      545,      574,      522,      574,
                              516,      545,      549,      548,      549,      546,
                              544,      544,      545,      547,      545,      554,
                              543,      548,      546,      583,      575,      511,
                              514,      571,      520,      545,      550,      543,
                              548,      547,      548,      551,      547,     1599,
                              547,      547,      547,      543,      546,     1603,
                              547,      544,      547,     1607,      541,      541,
                              558,      551,      537
                             };
int main(void)
{
    u8 i, key_val = 0;

    NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);
    delay_init();
    My_USART1_Init();
    Key_Init();
    LED_Init();
//    Remote_Init();
    IR_Init();

    while(1)
    {
        key_val = Key_Scan();
        if(key_val)
        {
            switch (key_val)
            {
            case 2:
            {
                for(i=0; i<242; i+=2)
                {
                    IR_Send(1, Code_OFF[i]);
                    IR_Send(0, Code_OFF[i+1]);
                }
                IR_Send(1, Code_OFF[242]);
                TIM_Cmd(TIM3, DISABLE);
                //		IR_Send(1, Code[241]);
                TIM3->CCR4 = 25;
                TIM_Cmd(TIM3, ENABLE);
								GPIO_WriteBit(GPIOC, GPIO_Pin_10, 0);
                break;
            }
            case 3:
            {
                for(i=0; i<242; i+=2)
                {
                    IR_Send(1, Code_ON[i]);
                    IR_Send(0, Code_ON[i+1]);
                }
                IR_Send(1, Code_ON[242]);
                TIM_Cmd(TIM3, DISABLE);
                //		IR_Send(1, Code[241]);
                TIM3->CCR4 = 25;
                TIM_Cmd(TIM3, ENABLE);
                break;
            }
            }
        }
    }
}

